﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace QunYing.DAL
{
    /// <summary>
    /// 数据访问类:OrderDetail
    /// </summary>
    public partial class OrderDetail
    {
        public OrderDetail()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int 详情ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from OrderDetail");
            strSql.Append(" where 详情ID=@详情ID");
            SqlParameter[] parameters = {
					new SqlParameter("@详情ID", SqlDbType.Int,4)
};
            parameters[0].Value = 详情ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(QunYing.Model.OrderDetail model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into OrderDetail(");
            strSql.Append("订单ID,商品ID,商品名称,商品品牌,单位,单价,积分,赠送积分,数量,类型)");
            strSql.Append(" values (");
            strSql.Append("@订单ID,@商品ID,@商品名称,@商品品牌,@单位,@单价,@积分,@赠送积分,@数量,@类型)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@订单ID", SqlDbType.Int,4),
					new SqlParameter("@商品ID", SqlDbType.Int,4),
					new SqlParameter("@商品名称", SqlDbType.NVarChar,100),
					new SqlParameter("@商品品牌", SqlDbType.NVarChar,20),
					new SqlParameter("@单位", SqlDbType.NVarChar,5),
					new SqlParameter("@单价", SqlDbType.Decimal,13),
					new SqlParameter("@积分", SqlDbType.Int,4),
					new SqlParameter("@赠送积分", SqlDbType.Int,4),
					new SqlParameter("@数量", SqlDbType.Int,4),
					new SqlParameter("@类型", SqlDbType.NVarChar,10)};
            parameters[0].Value = model.订单ID;
            parameters[1].Value = model.商品ID;
            parameters[2].Value = model.商品名称;
            parameters[3].Value = model.商品品牌;
            parameters[4].Value = model.单位;
            parameters[5].Value = model.单价;
            parameters[6].Value = model.积分;
            parameters[7].Value = model.赠送积分;
            parameters[8].Value = model.数量;
            parameters[9].Value = model.类型;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(QunYing.Model.OrderDetail model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update OrderDetail set ");
            strSql.Append("订单ID=@订单ID,");
            strSql.Append("商品ID=@商品ID,");
            strSql.Append("商品名称=@商品名称,");
            strSql.Append("商品品牌=@商品品牌,");
            strSql.Append("单位=@单位,");
            strSql.Append("单价=@单价,");
            strSql.Append("积分=@积分,");
            strSql.Append("赠送积分=@赠送积分,");
            strSql.Append("数量=@数量,");
            strSql.Append("类型=@类型");
            strSql.Append(" where 详情ID=@详情ID");
            SqlParameter[] parameters = {
					new SqlParameter("@订单ID", SqlDbType.Int,4),
					new SqlParameter("@商品ID", SqlDbType.Int,4),
					new SqlParameter("@商品名称", SqlDbType.NVarChar,100),
					new SqlParameter("@商品品牌", SqlDbType.NVarChar,20),
					new SqlParameter("@单位", SqlDbType.NVarChar,5),
					new SqlParameter("@单价", SqlDbType.Decimal,13),
					new SqlParameter("@积分", SqlDbType.Int,4),
					new SqlParameter("@赠送积分", SqlDbType.Int,4),
					new SqlParameter("@数量", SqlDbType.Int,4),
					new SqlParameter("@类型", SqlDbType.NVarChar,10),
					new SqlParameter("@详情ID", SqlDbType.Int,4)};
            parameters[0].Value = model.订单ID;
            parameters[1].Value = model.商品ID;
            parameters[2].Value = model.商品名称;
            parameters[3].Value = model.商品品牌;
            parameters[4].Value = model.单位;
            parameters[5].Value = model.单价;
            parameters[6].Value = model.积分;
            parameters[7].Value = model.赠送积分;
            parameters[8].Value = model.数量;
            parameters[9].Value = model.类型;
            parameters[10].Value = model.详情ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int 详情ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from OrderDetail ");
            strSql.Append(" where 详情ID=@详情ID");
            SqlParameter[] parameters = {
					new SqlParameter("@详情ID", SqlDbType.Int,4)
};
            parameters[0].Value = 详情ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string 详情IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from OrderDetail ");
            strSql.Append(" where 详情ID in (" + 详情IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public QunYing.Model.OrderDetail GetModel(int 详情ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 详情ID,订单ID,商品ID,商品名称,商品品牌,单位,单价,积分,赠送积分,数量,类型 from OrderDetail ");
            strSql.Append(" where 详情ID=@详情ID");
            SqlParameter[] parameters = {
					new SqlParameter("@详情ID", SqlDbType.Int,4)
};
            parameters[0].Value = 详情ID;

            QunYing.Model.OrderDetail model = new QunYing.Model.OrderDetail();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["详情ID"] != null && ds.Tables[0].Rows[0]["详情ID"].ToString() != "")
                {
                    model.详情ID = int.Parse(ds.Tables[0].Rows[0]["详情ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["订单ID"] != null && ds.Tables[0].Rows[0]["订单ID"].ToString() != "")
                {
                    model.订单ID = int.Parse(ds.Tables[0].Rows[0]["订单ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["商品ID"] != null && ds.Tables[0].Rows[0]["商品ID"].ToString() != "")
                {
                    model.商品ID = int.Parse(ds.Tables[0].Rows[0]["商品ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["商品名称"] != null && ds.Tables[0].Rows[0]["商品名称"].ToString() != "")
                {
                    model.商品名称 = ds.Tables[0].Rows[0]["商品名称"].ToString();
                }
                if (ds.Tables[0].Rows[0]["商品品牌"] != null && ds.Tables[0].Rows[0]["商品品牌"].ToString() != "")
                {
                    model.商品品牌 = ds.Tables[0].Rows[0]["商品品牌"].ToString();
                }
                if (ds.Tables[0].Rows[0]["单位"] != null && ds.Tables[0].Rows[0]["单位"].ToString() != "")
                {
                    model.单位 = ds.Tables[0].Rows[0]["单位"].ToString();
                }
                if (ds.Tables[0].Rows[0]["单价"] != null && ds.Tables[0].Rows[0]["单价"].ToString() != "")
                {
                    model.单价 = decimal.Parse(ds.Tables[0].Rows[0]["单价"].ToString());
                }
                if (ds.Tables[0].Rows[0]["积分"] != null && ds.Tables[0].Rows[0]["积分"].ToString() != "")
                {
                    model.积分 = int.Parse(ds.Tables[0].Rows[0]["积分"].ToString());
                }
                if (ds.Tables[0].Rows[0]["赠送积分"] != null && ds.Tables[0].Rows[0]["赠送积分"].ToString() != "")
                {
                    model.赠送积分 = int.Parse(ds.Tables[0].Rows[0]["赠送积分"].ToString());
                }
                if (ds.Tables[0].Rows[0]["数量"] != null && ds.Tables[0].Rows[0]["数量"].ToString() != "")
                {
                    model.数量 = int.Parse(ds.Tables[0].Rows[0]["数量"].ToString());
                }
                if (ds.Tables[0].Rows[0]["类型"] != null && ds.Tables[0].Rows[0]["类型"].ToString() != "")
                {
                    model.类型 = ds.Tables[0].Rows[0]["类型"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select 详情ID,订单ID,商品ID,商品名称,商品品牌,单位,单价,积分,赠送积分,数量,类型 ");
            strSql.Append(" FROM OrderDetail ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" 详情ID,订单ID,商品ID,商品名称,商品品牌,单位,单价,积分,赠送积分,数量,类型 ");
            strSql.Append(" FROM OrderDetail ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "OrderDetail";
            parameters[1].Value = "详情ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

